$(document).ready(function() {

	$("#fromDate").focus(function() {
		WdatePicker({realDateFmt:'yyyy-MM-dd HH:mm', dateFmt:'yyyy-MM-dd HH:mm', minDate:'%y-%M-%d', maxDate:'#F{$dp.$D(\'toDate\',{d:0})}'});
//		$(this).blur();				//选择后使之失去焦点，避免出现点击两次的情况
	});
	$("#toDate").focus(function() {
		WdatePicker({realDateFmt:'yyyy-MM-dd HH:mm', dateFmt:'yyyy-MM-dd HH:mm', minDate:'#F{$dp.$D(\'fromDate\',{d:0})}'});
//		$(this).blur();				//选择后使之失去焦点，避免出现点击两次的情况
	});
	
	$("#carDetailID").change(function() {
		var carId = $("#carDetailID").val();
		if (carId == -1) {
			$("#loadMax").val("");
			$("#defaultContactDisplay").val("");
		} else {
			$.ajax({
				type : "post", // 请求方式为POST，避免乱码问题
				url : "car/prePublishCarSelectCar.action", // 发送请求地址
				data : { // 发送给数据库的数据
					carId : carId,
					ajax : "true"
				},
				dataType : "json", // 接收的数据格式为JSON，如果不指定则自动检测
				success : function(jsondata, textStatus) {
					$("#loadMax").val(jsondata["loadMax"]);
					$("#defaultContactDisplay").val(jsondata["defaultContact"]);
					$("#defaultContactCommitted").val(jsondata["defaultContact"]);
				}
			});
		}
	});

	$("#error_mark").hide();
	$("#submit_button").click(function() {
		check_form();
	});
	
	$("#xx1").css("width", $("#td1width").css("width"));
	$("#xx2").css("width", $("#td1width").css("width"));
	$("#xx3").css("width", $("#td1width").css("width"));
	
//	blur_check();
//	$("#carryingNum").blur(function() {
//		if ($.trim($("#carryingNum").val()) == "") {//  || $("#deadWeight").val() - $("#carryingNum").val() < 0
//			$("#remainingWeight").val("");
//		} else {
//			$("#remainingWeight").val($("#deadWeight").val() - $("#carryingNum").val());
//		}
//	});
//	
//	<tr id="toArea2" style="display:none;">
//	<td id="xx3"><span class="red">* </span>目的地(附加)：</td>
//	<td id="toArea">
//		<huoyun:regionSelect provinceName="carRouteToB.province" cityName="carRouteToB.city" townName="carRouteToB.town" />
//		<a href="javascript:;" id="deleteToAreaHref2">删除</a>
//	</td>
//</tr>
	
	
	$("#addAreaHref").click(function() {
		if($("#Area2").is(":hidden")) {
			$("#Area2").show();
		}else if($("#Area3").is(":hidden")) {
			$("#Area3").show();
			$("#addAreaHref").hide();
		}
	});
	$("#deleteAreaHref2").click(function() {
		$("#carRouteBstartProvince option[value='']").attr("selected",true);
		$("#carRouteBstartProvince").attr("value","");
		$("#carRouteBstartCity option[value='']").attr("selected",true);
		$("#carRouteBstartCity").attr("value","");
		$("#carRouteBstartTown option[value='']").attr("selected",true);
		$("#carRouteBstartTown").attr("value","");
		$("#carRouteBendProvince option[value='']").attr("selected",true);
		$("#carRouteBendProvince").attr("value","");
		$("#carRouteBendCity option[value='']").attr("selected",true);
		$("#carRouteBendCity").attr("value","");
		$("#carRouteBendTown option[value='']").attr("selected",true);
		$("#carRouteBendTown").attr("value","");
		$("#Area2").hide();
		$("#addAreaHref").show();
	});
	$("#deleteAreaHref3").click(function() {
		$("#carRouteCstartProvince option[value='']").attr("selected",true);
		$("#carRouteCstartProvince").attr("value","");
		$("#carRouteCstartCity option[value='']").attr("selected",true);
		$("#carRouteCstartCity").attr("value","");
		$("#carRouteCstartTown option[value='']").attr("selected",true);
		$("#carRouteCstartTown").attr("value","");
		$("#carRouteCendProvince option[value='']").attr("selected",true);
		$("#carRouteCendProvince").attr("value","");
		$("#carRouteCendCity option[value='']").attr("selected",true);
		$("#carRouteCendCity").attr("value","");
		$("#carRouteCendTown option[value='']").attr("selected",true);
		$("#carRouteCendTown").attr("value","");
		$("#Area3").hide();
		$("#deleteAreaHref2").show();
		$("#addAreaHref").show();
	});
});


var $fromAreaCount = 0;
var $toAreaCount = 0;
var $fromAreaTotal = 0;
var $toAreaTotal = 0;

function check_form() {
	$("#hint").html("");
	$("*").css("borderColor","");
	$("#error_mark").hide();
	
	if (!check_carDetailId()) {
		$("#error_mark").show();
		$("#hint").html("<font size='3px'>请选择已有车辆！</font><br><br>");
		return false;
	}
	if (!check_fromInfo()) {
		$("#error_mark").show();
		$("#hint").html("<font size='3px'>请填写完整起始地信息！</font><br><br>");
		return false;
	}
	if (!check_toInfo()) {
		$("#error_mark").show();
		$("#hint").html("<font size='3px'>请填写完整目的地信息！</font><br><br>");
		return false;
	}
	if (!check_timePriod()) {
		$("#error_mark").show();
		$("#hint").html("<font size='3px'>请填写完整发车区间！</font><br><br>");
		return false;
	}
//	if (check_carryingNum() != 0) {
//		$("#error_mark").show();
//		if (check_carryingNum() == -1) {
//			$("#hint").html("<font size='3px'>请填写已载货物吨位！</font><br><br>");
//		} else {
//			$("#hint").html("<font size='3px'>已载货物吨位不能大于最大可载吨位！</font><br><br>");
//		}
//		return false;
//	}
	
	carInfo.submit();
	return true;
}

function check_carDetailId() {
	if($("#carDetailID").val() == -1) {
		$("#carDetailID").css("border","1px solid red");
		return false;
	} else {
		$("#carDetailID").css("border","");
		$("#hint").html("");
		return true;
	}
}
function check_fromInfo() {
//	if ($.trim($("#fromProvince").val()) == "选择省" || $.trim($("#fromCity").val()) == "选择市" || $.trim($("#fromTown").val()) == "选择区") {
//		$("#hint").html("请填写完整起始地！<br><br>");
//		
//		
//		
//		return false;
//	} else {
//		$("#fromProvince").css("border","");
//		$("#fromCity").css("border","");
//		$("#fromTown").css("border","");
//		$("#hint").html("");
//	}
	if (!check_fromProvince()) {
		return false;
	}
	if (!check_fromCity()) {
		return false;
	}
	if (!check_fromTown()) {
		return false;
	}
	return true;
}

function check_fromProvince() {
	if ($.trim($("#fromProvince").val()) == "选择省") {
		$("#fromProvince").css("border","1px solid red");
		return false;
	} else {
		$("#fromProvince").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_fromCity() {
	if ($.trim($("#fromCity").val()) == "选择市") {
		$("#fromCity").css("border","1px solid red");
		return false;
	} else {
		$("#fromCity").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_fromTown() {
	if ($.trim($("#fromTown").val()) == "选择区") {
		$("#fromTown").css("border","1px solid red");
		return false;
	} else {
		$("#fromTown").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_toInfo() {
//	if ($.trim($("#toProvince").val()) == "选择省" || $.trim($("#toCity").val()) == "选择市" || $.trim($("#toTown").val()) == "选择区") {
//		$("#hint").html("请填写完整目的地！<br><br>");
//		
//		
//		
//		return false;
//	} else {
//		$("#toProvince").css("border","");
//		$("#toCity").css("border","");
//		$("#toTown").css("border","");
//		$("#hint").html("");
//	}
	if (!check_toProvince()) {
		return false;
	}
	if (!check_toCity()) {
		return false;
	}
	if (!check_toTown()) {
		return false;
	}
	return true;
}

function check_toProvince() {
	if ($.trim($("#toProvince").val()) == "选择省") {
		$("#toProvince").css("border","1px solid red");
		return false;
	} else {
		$("#toProvince").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_toCity() {
	if ($.trim($("#toCity").val()) == "选择市") {
		$("#toCity").css("border","1px solid red");
		return false;
	} else {
		$("#toCity").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_toTown() {
	if ($.trim($("#toTown").val()) == "选择区") {
		$("#toTown").css("border","1px solid red");
		return false;
	} else {
		$("#toTown").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_timePriod() {
//	if ($("#fromDate").val() == "" || $("#toDate").val() == "") {
//		$("#hint").html("请填写完整发车区间！<br><br>");
//		
//		
//		return false;
//	} else {
//		$("#fromDate").css("border","");
//		$("#toDate").css("border","");
//		$("#hint").html("");
//	}
	if (!check_startTime()) {
		return false;
	}
	if (!check_endTime()) {
		return false;
	}
	return true;
}

function check_startTime() {
	if ($("#fromDate").val() == "") {
		$("#fromDate").css("border","1px solid red");
		return false;
	} else {
		$("#fromDate").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_endTime() {
	if ($("#toDate").val() == "") {
		$("#toDate").css("border","1px solid red");
		return false;
	} else {
		$("#toDate").css("border","");
		$("#hint").html("");
		return true;
	}
}

function check_carryingNum() {
	if ($.trim($("#carryingNum").val()) == "" || $("#carryingNum").val() - $("#deadWeight").val() > 0) {
		if ($.trim($("#carryingNum").val()) == "") {
			$("#carryingNum").css("border","1px solid red");
			return -1;
		} else {
			$("#carryingNum").css("border","1px solid red");
			return -2;
		}
	} else {
		$("#carryingNum").css("border","");
		$("#hint").html("");
		return 0;
	}
}

function check_emergencyTelephone() {
	if($.trim($("#emergencyTelephone").val()) == "") {
		$("#hint").html("请填写紧急联系方式！<br><br>");
		$("#emergencyTelephone").css("border","1px solid red");
		return false;
	} else {
		$("#emergencyTelephone").css("border","");
		$("#hint").html("");
		return true;
	}
}

function blur_check() {
	$("#carDetailID").blur(function() {
		check_carDetailId();
	});
	
	$("#fromProvince").blur(function() {
		check_fromProvince();
	});
	
	$("#fromCity").blur(function() {
		check_fromCity();
	});
	
	$("#fromTown").blur(function() {
		check_fromTown();
	});
	
	$("#toProvince").blur(function() {
		check_toInfo();
	});
	
	$("#toCity").blur(function() {
		check_toInfo();
	});
	
	$("#toTown").blur(function() {
		check_toInfo();
	});
	
	$("#fromDate").blur(function() {
		check_startTime();
	});
	
	$("#toDate").blur(function() {
		check_endTime();
	});
	
	$("#carryingNum").blur(function() {
		check_carryingNum();
	});
	
	$("#emergencyTelephone").blur(function() {
		
	});
	
	$("#carInfo").validate({
	  	errorPlacement: function(error, element) {
			// element.parent("td").append(error);
				element.attr("title", error.html());
			},
	  	rules : {
	  		"carPubInfo.carId" : {
	  			required : true,
	  			isValid : true
	  		},
	  		"carRouteA.startProvince":{
	  			required : true
	  		},
	  		"carRouteA.startCity":{
	  			required : true
	  		},
	  		"carRouteA.startTown":{
	  			required : true
	  		},
	  		"carRouteA.endProvince":{
	  			required : true
	  		},
	  		"carRouteA.endCity":{
	  			required : true
	  		},
	  		"carRouteA.endTown":{
	  			required : true
	  		},
	  		"carPubInfo.starttime":{
	  			required : true,
	  			moreThanToday:true
	  		},
	  		"carPubInfo.endtime" : {
					required : true,
					moreThanStartDate : "carPubInfo.starttime"
			}
	  	},
	  	messages : {
	  		"carPubInfo.carId" : {
	  			required : "请输入车牌号",
	  			isValid : "请输入有效的车牌号"
	  		},
	  		"carRouteA.startProvince":{
	  			required : "请选择出发省市"
	  		},
	  		"carRouteA.startCity":{
	  			required : "请选择出发者所在的市区"
	  		},
	  		"carRouteA.startTown":{
	  			required : "请选择出发者所在的县区"
	  		},
	  		"carRouteA.endProvince":{
	  			required : "请选择目的省市"
	  		},
	  		"carRouteA.endCity":{
	  			required : "请选择目的地所在的市区"
	  		},
	  		"carRouteA.endTown":{
	  			required : "请选择目的地所在的县区"
	  		},
	  		"carPubInfo.starttime":{
	  			required : "请输入出发时间",
	  			moreThanToday:"请输入大于今日的时间"
	  		},
	  		"carPubInfo.endtime" : {
				required : "请输入到达目的地时间",
				moreThanStartDate : "请输入大于出发日期的时间"
			}
	  	}
	  });
}